<?php

trait Trait_Api_Destination
{


    /**
     * 递归函数查询下级
     * @param $result
     * @param $pid
     * @param $data
     */
    private function get_child($result, $pid, &$data)
    {
        $data['litpic'] = $this->img_url($data['litpic']);
        foreach ($result as $k => $item) {
            if ($item['pid'] == $pid) {
                unset($result[$k]);
                $this->get_child($result, $item['id'], $item);
                $data['child'][] = $item;
            }
        }
    }

    /**
     * 按目的地获取产品列表
     * @param $type_id_list 主导航开启的type_id
     * @param $dest_id 目的地id
     * @return array
     * User: netman
     * Date: 2023/9/20 13:43
     */
    private function get_product_by_dest_id($type_id_list, $dest_id)
    {
        //产品
        $ids = array(1, 2, 5, 3, 114, 21, 13, 118);
        $arr = array();
        foreach ($type_id_list as $item) {
            if (in_array($item['m_typeid'], $ids)) {
                $arr[] = $item['m_typeid'];
            }
        }
        $data = array();
        $now_time = time();
        if (count($arr)) {
            $ar = DB::select('id', 'maintable', 'modulename')->from('model')->where('id', 'in', $arr)->execute()->as_array();
            foreach ($ar as $item) {
                if (!St_Functions::checkModuleOpen($item['id'])) {
                    continue;
                }
                $type_id             = $item['id'];
                $main_table          = $item['maintable'];
                $main_table_fullname = 'sline_' . $main_table;

                $fields = array('a.id', 'a.title', 'a.litpic', 'a.price', 'a.bookcount');
                //团购
                if ($type_id == 13) {
                    $fields = array('a.id', 'a.title', 'a.litpic', 'a.price', 'a.sellprice', 'a.starttime', 'a.endtime', 'a.totalnum');
                }

                $builder = DB::select_array($fields)
                    ->from(array($main_table, 'a'))
                    ->join('kindorderlist', 'left')
                    ->on('a.id', '=', 'kindorderlist.aid')
                    ->on('kindorderlist.typeid', '=', (int)$type_id)
                    ->on('kindorderlist.classid', '=', (int)$dest_id)
                    ->where('a.ishidden', '=', 0)
                    ->and_where(DB::expr(" AND FIND_IN_SET({$dest_id},sline_a.kindlist)"));

                if($type_id == 13) {
                    $builder->and_where('a.endtime', '>', $now_time);
                }

                $list = $builder->order_by(DB::expr(" IFNULL(sline_kindorderlist.displayorder,9999) ASC,sline_a.modtime DESC,sline_a.addtime DESC"))
                    ->limit(4)
                    ->execute()
                    ->as_array();
                if ($type_id == 13) {
                    //团购
                    foreach ($list as &$tuan) {
                        $tuan['litpic']   = $this->img_url($tuan['litpic']);
                        $tuan['discount'] = sprintf('%.1f', $tuan['price'] * 10 / $tuan['sellprice']);
                        if ($tuan['starttime'] > $now_time) {
                            //未开始
                            $tuan['status']   = 1;
                            $tuan['str_time'] = $this->caclulate_time($now_time, $tuan['starttime']);//剩余时间
                        } elseif ($tuan['endtime'] >= $now_time) {
                            //活动中
                            $tuan['status']   = 2;
                            $tuan['str_time'] = $this->caclulate_time($now_time, $tuan['endtime']);//剩余时间
                        } else {
                            //结束
                            $tuan['status'] = 3;
                        }
                    }
                } else {
                    foreach ($list as &$product) {
                        $product['litpic']  = Model_Api_V2_System::uniapp_img($product['litpic']);
                        $product['sellnum'] = Model_Member_Order::get_sell_num($product['id'], $item['id']) + (int)$product['bookcount'];
                    }
                }
                $d['type_id'] = $item['id'];
                $d['model']   = $item['maintable'];
                $d['name']    = $item['modulename'];
                $d['list']    = $list;
                $data[]       = $d;
            }
        }
        return $data;

    }

    /**
     * 按目的地获取文章列表
     * @param $type_id_list 主导航开启的type_id
     * @param $dest_id 目的地id
     * User: netman
     * Date: 2023/9/20 13:43
     */
    private function get_article_by_dest_id($type_id_list, $dest_id)
    {
        $ids = array(4, 101);
        $arr = array();
        foreach ($type_id_list as $item) {
            if (in_array($item['m_typeid'], $ids)) {
                $arr[] = $item['m_typeid'];
            }
        }
        $data = array();
        if (count($arr)) {
            $ar = DB::select('id', 'maintable', 'modulename')->from('model')->where('id', 'in', $arr)->execute()->as_array();
            foreach ($ar as $item) {

                $d['type_id'] = $item['id'];
                $d['model']   = $item['maintable'];
                $d['name']    = $item['modulename'];
                if ($d['type_id'] == 101) {
                    //游记
                    $model = DB::select('id', 'title', 'litpic', 'addtime', 'shownum', 'description')->from($item['maintable'])->where('status', '=', 1);
                } elseif ($d['type_id'] == 4) {
                    //文章攻略
                    $model = DB::select('id', 'title', 'litpic', 'addtime', 'shownum', 'summary')->from($item['maintable'])->where('ishidden', '=', 0);
                }
                $d['list'] = $model->and_where(DB::expr('find_in_set(' . $dest_id . ',kindlist)'), '>', 0)->limit(4)->execute()->as_array();
                foreach ($d['list'] as &$article) {
                    if (isset($article['litpic'])) {
                        $article['litpic'] = $this->img_url($article['litpic']);
                    }
                    $article['addtime'] = date('Y-m-d', $article['addtime']);
                }
                $data[] = $d;
            }
        }
        return $data;

    }

    /**
     * 计算时分秒
     * @param $time
     * @return string
     */
    private function caclulate_time($startdate, $end)
    {

        $date = floor(($end - $startdate) / 86400);

        $hour = floor(($end - $startdate) % 86400 / 3600);

        $minute = floor(($end - $startdate) % 86400 / 60);

        $second = floor(($end - $startdate) % 86400 % 60);
        return "{$date}天{$hour}时{$minute}分{$second}秒";
    }

    /**
     * 获取热门目的地
     * @param $fields
     * @param $params
     * @return array
     */
    private function get_hot_dest($fields, $params)
    {
        $builder = DB::select_array($fields)->from('destinations')
            ->where('isopen', '=', 1)
            ->and_where('pid', '!=', 0)
            ->and_where('ishot', '=', 1);
        if ($params['type_id']) {
            $builder->and_where(DB::expr(" and FIND_IN_SET({$params['type_id']}, opentypeids)"));
        }
        if ($params['keyword']) {
            $builder->where_open();
            $builder->where('kindname', 'like', "%{$params['keyword']}%");
            $builder->or_where('pinyin', 'like', "%{$params['keyword']}%");
            $builder->where_close();
        }
        $builder->group_by('id')->order_by('displayorder', 'asc');
        $result = $builder->execute()->as_array();

        foreach ($result as &$v) {
            $v['litpic'] = $this->img_url($v['litpic']);
        }

        return [
            'id'           => 0,
            'kindname'     => '热门',
            'pinyin'       => '',
            'pid'          => 0,
            'displayorder' => 1,
            'litpic'       => '',
            'child'        => [
                [
                   'id'           => 0,
                   'kindname'     => '热门目的地',
                   'pinyin'       => '',
                   'pid'          => 0,
                   'displayorder' => 1,
                   'litpic'       => '',
                   'child'        => $result
                ]
            ]
        ];
    }
}